package com.zlzf.helloworld.qq;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.zlzf.helloworld.R;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class qqctivity extends AppCompatActivity {

    private ListView listView;
    private ArrayList<String> mData;

    // 继续有多少个条目的delete被展示出来的集合
    private List<SlideDelete> slideDeleteArrayList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qqctivity);
        listView = (ListView) findViewById(R.id.lvSlide);
        mData = new ArrayList<>();

        for(int i=0;i<200;i++){
            mData.add("文本"+i);
        }
        listView.setAdapter(new MyAdapter());
    }

    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int i) {
            return mData.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            if(view == null){
                viewHolder = new ViewHolder();
                view = View.inflate(qqctivity.this,R.layout.silde_item,null);
                viewHolder.mSlideDelete = (SlideDelete) view.findViewById(R.id.mSlideDelete);
                viewHolder.mTvContent = (TextView) view.findViewById(R.id.tvContent);
                viewHolder.mLlDelete = (LinearLayout) view.findViewById(R.id.delete);
                view.setTag(viewHolder);

            }else{
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.mTvContent.setText(mData.get(i));

            viewHolder.mSlideDelete.setOnSlideDeleteListener(new SlideDelete.OnSlideDeleteListener() {
                @Override
                public void onOpen(SlideDelete slideDelete) {
                    closeOtherItem();

                    slideDeleteArrayList.add(slideDelete);
                    Log.d("Slide", "slideDeleteArrayList当前数量：" + slideDeleteArrayList.size());
                }

                @Override
                public void onClose(SlideDelete slideDelete) {
                    slideDeleteArrayList.remove(slideDelete);
                    Log.d("Slide", "slideDeleteArrayList当前数量：" + slideDeleteArrayList.size());

                }
            });

            viewHolder.mLlDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mData.remove(i);
                    notifyDataSetChanged();
                }
            });

            return view;
        }
    }

    class ViewHolder{
        SlideDelete mSlideDelete;
        TextView mTvContent;
        LinearLayout mLlDelete;
    }

    private void closeOtherItem(){
        // 采用Iterator的原因是for是线程不安全的，迭代器是线程安全的
        ListIterator<SlideDelete> slideDeleteListIterator = slideDeleteArrayList.listIterator();
        while(slideDeleteListIterator.hasNext()){
            SlideDelete slideDelete = slideDeleteListIterator.next();
            slideDelete.isShowDelete(false);
        }
        slideDeleteArrayList.clear();
    }
}
